package configuration

import (
	"administrator/app/apputils/tokenutils"
	"administrator/app/mapper"
	"administrator/app/model"
	"errors"
	"gitee.com/aurora-engine/utils/rep"
	"gitee.com/aurora-engine/utils/treeutils"
	"gitee.com/aurora-engine/web"
)

type MenuManagement struct {
	*mapper.MenuManageMapper
	*mapper.RoleManageMapper
}

func NewMenuManagement() web.Constructor {
	return func() any {
		return &MenuManagement{}
	}
}

// Add
// @Description :
// @Param route :
// [Post] => /api/configuration/menu/add
func (manage *MenuManagement) Add(route model.MenuRouter) (rep.Response, error) {
	var id int64
	var err error
	if _, id, err = manage.AddRoute(route); err != nil {
		return rep.Fail(err, "添加失败"), err
	}
	route.Id = int(id)
	var empty treeutils.Empty[int] = &route
	node := treeutils.TNode[int]{
		Empty: empty,
		Child: []*treeutils.TNode[int]{},
	}
	return rep.Success(node, "添加成功"), nil
}

// Tree
// @Description : 用于加载初始化路由信息
// @Param args :
// [Get] => /api/configuration/menu/list
func (manage *MenuManagement) Tree(args map[string]int) (rep.Response, error) {
	var list []*model.MenuRouter
	var err error

	if list, err = manage.SelectAllMenu(); err != nil {
		return rep.Fail(err, "菜单查询失败"), err
	}
	tree := treeutils.ChildTree(args["pid"], list)
	return rep.Success(tree), nil
}

// GetRoleTree
// @Description :
// @Param args :
// @Param token :
// [Get] => /api/configuration/menu/role/tree
func (manage *MenuManagement) GetRoleTree(args map[string]any, token *tokenutils.Token) (rep.Response, error) {
	var list []*model.MenuRouter
	var err error
	args["userId"] = token.ID
	// 更具菜单权限 查询指定菜单
	if list, err = manage.SelectRoleMenus(args); err != nil {
		return rep.Fail(err, "菜单查询失败"), err
	}
	pid := 0
	if list != nil && len(list) > 0 {
		pid = list[0].Pid
	}
	tree := treeutils.ChildTree(pid, list)
	return rep.Success(tree), nil
}

// GetOurTree
// @Description : 用于查询登录者菜单展示信息
// @Param args :
// @Param token :
// [Get] => /api/configuration/menu/our/tree
func (manage *MenuManagement) GetOurTree(args map[string]any, token *tokenutils.Token) (rep.Response, error) {
	var list []*model.MenuRouter
	var err error
	args["userId"] = token.ID
	// 更具菜单权限 查询指定菜单
	if list, err = manage.SelectOurMenus(args); err != nil {
		return rep.Fail(err, "菜单查询失败"), err
	}
	pid := 0
	if list != nil && len(list) > 0 {
		pid = list[0].Pid
	}
	tree := treeutils.ChildTree(pid, list)
	return rep.Success(tree), nil
}

// MenuList
// @Description :
// @Param args :
// @Param token :
// [Get] => /api/configuration/menu/our/menuList
func (manage *MenuManagement) MenuList(args map[string]any, token *tokenutils.Token) (rep.Response, error) {
	var list []*model.MenuRouter
	var err error
	args["userId"] = token.ID
	// 更具菜单权限 查询指定菜单
	if list, err = manage.SelectOurMenus(args); err != nil {
		return rep.Fail(err, "菜单查询失败"), err
	}
	return rep.Success(list), nil
}

// List
// @Description : 公共获取 全部路由信息便于前端加载路由
// @Param args :
// [Get] => /public/menu
func (manage *MenuManagement) List(args map[string]int) (rep.Response, error) {
	var list []*model.MenuRouter
	var err error
	if list, err = manage.SelectAllMenu(); err != nil {
		return rep.Fail(err, "菜单查询失败"), err
	}
	return rep.Success(list), nil
}

// Delete
// @Description :
// @Param args :
// [Post] => /api/configuration/menu/del
func (manage *MenuManagement) Delete(args map[string]int) (rep.Response, error) {
	var err error
	// 校验菜单是否启用 启用状态不可删除

	if err = manage.DelMenu(args); err != nil {
		return rep.Fail(err, "删除失败"), err
	}
	return rep.Success(true), nil
}

// ModifyStatus
// @Description :
// @Param args :
// [Post] => /api/configuration/menu/status
func (manage *MenuManagement) ModifyStatus(args map[string]any) (rep.Response, error) {
	var pid int
	var err error
	if _, flag := args["pid"]; !flag {
		err = errors.New("pid 不存在")
		return rep.Fail(err, "修改失败"), err
	}
	pid = int(args["pid"].(float64))
	// 查找 pid 及其子节点 id
	var list []*model.MenuRouter
	if list, err = manage.SelectAllMenu(); err != nil {
		return rep.Fail(err, "菜单查询失败"), err
	}
	ids := treeutils.GetChildIds(list, pid)
	// 更新 ids 的菜单状态
	args["ids"] = ids
	if err = manage.Status(args); err != nil {
		return rep.Fail(err, "修改失败"), err
	}
	return rep.Success(true), nil
}
